package bavaria.hightech.banking.statement;

import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.Iterator;

import bavaria.hightech.banking.accounts.Account;
import bavaria.hightech.banking.lists.Booking;
import bavaria.hightech.banking.resourceBundles.LanguageBundle;

/**
 * class KtoAuszugsFormiererPlainTest implements KtoAuszugsFormartierer
 * @author David
 *
 */
public class KtoAuszugsFormatiererPlainTextImpl implements
		KtoAuszugsFormatierer {

	Iterator<Booking> iterator;
	LanguageBundle languageBundle;

	@Override
	public void specifyAccount(Account acc) {
		iterator = acc.getBookingsListIterator();
		languageBundle = acc.languageBundle;
	}

	@Override
	public String getAuszug() {
		Booking current;
		String auszug = new String();
		while (iterator.hasNext()) {
			current = iterator.next();
			auszug += formatOneField(current.getBookingTime(),
					current.getAmount(), current.getReason());
		}
		auszug += "\n";
		return auszug;
	}

	/**
	 * formate one field
	 * @param bookingTime
	 * @param amount
	 * @param reason
	 * @return
	 */
	private String formatOneField(Date bookingTime, long amount, String reason) {
		NumberFormat nFormatter = NumberFormat.getNumberInstance(languageBundle
				.getLocale());
		String formattedAmount = nFormatter.format(amount);
		String formattedDate = DateFormat.getDateInstance(DateFormat.MEDIUM,
				languageBundle.getLocale()).format(bookingTime);
		String plain = formattedDate + " | {13}: "
				+ ((amount <= 0) ? "{14}" : "{15}") + " | {16}: "
				+ formattedAmount + " | {17}: " + reason + "\n";
		return MessageFormat.format(plain, languageBundle.getContents());

	}

}
